package org.xu.logic;

import com.core.utils.HttpFormUtil;
import com.core.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.xu.entity.WorkFlowLogEntity;
import org.xu.enums.WorkTypeEnum;
import org.xu.service.WorkFlowLogService;

import java.util.Date;

@Slf4j
@Service
public class AwardLogicImpl implements AwardLogic{

    @Autowired
    private WorkFlowLogService workFlowLogService;
    private static final String dataIdStr = "signIn_Award";

    public void award(String key,String token) {
        StringBuffer sbf = new StringBuffer();
        sbf.append("------WebKitFormBoundaryOzMSoaYToZQxgiBc\r\n")
                .append("Content-Disposition: form-data; name=\"token\"\r\n")
                .append("\r\n")
                .append(token.trim())
                .append("\r\n------WebKitFormBoundaryOzMSoaYToZQxgiBc--");
     //   System.out.println("获取点击次数，入参:");
     //   System.out.println(sbf.toString());
        String urlString = "https://h5up.bluebirdh5.top/sq/YxDecennial/getDrawsNum";
        String timesResponse = HttpFormUtil.formCommit("获取点击次数",urlString, sbf.toString(), "----WebKitFormBoundaryOzMSoaYToZQxgiBc");
        if (!StringUtils.isEmpty(timesResponse) && !timesResponse.contains("失败")){
            // 构建表单数据
            StringBuffer getAwardsSbf = new StringBuffer();
            getAwardsSbf.append("------WebKitFormBoundaryz8FRui0P4tzY4dKB\r\n")
                    .append("Content-Disposition: form-data; name=\"token\"\r\n")
                    .append("\r\n")
                    .append(token.trim())
                    .append("\r\n------WebKitFormBoundaryz8FRui0P4tzY4dKB--");
          //  System.out.println(getAwardsSbf.toString());
            // 点击抽奖
            String getAwardsUrlStr = "https://h5up.bluebirdh5.top/sq/YxDecennial/getAwards";
            String awardsResponse = HttpFormUtil.formCommit("点击抽奖",getAwardsUrlStr, getAwardsSbf.toString(), "----WebKitFormBoundaryz8FRui0P4tzY4dKB");
            if (!"失败".equals(awardsResponse)) {
                WorkFlowLogEntity entity = new WorkFlowLogEntity();
                entity.setId(UUIDGenerator.generate());
                entity.setDataId(dataIdStr);
                entity.setApplyNo("test_"+key);
                entity.setCreateTime(new Date());
                entity.setOperationResult(key+":转盘获取完成");
                entity.setRemark(awardsResponse);
                entity.setDeleted(false);
                entity.setWorkType(WorkTypeEnum.SIGN);
                workFlowLogService.insert(entity);
            }
        }
    }


}
